using DG.Tweening;
using UnityEngine;
using UnityEngine.Events;

namespace Core.Extension.UIAnimation
{
    /// <summary>
    /// 封装的动画函数
    /// </summary>
    public static class AnimationFunction
    {
        public static Tweener WindowScale(Transform moveAbleDialogue, UIAnimationData data, UnityAction complete = null)
        {
            if (data.timer==0)
            {
                complete?.Invoke();
                return null;
            }
            //位置下移
            moveAbleDialogue.DOKill();
            moveAbleDialogue.transform.localScale = data.start;
            var tween = moveAbleDialogue.DOScale(data.end, data.timer).SetLoops(data.isLoop,data.loopType);
            tween.SetEase(data.easeType).SetDelay(data.delay).onComplete = () => { complete?.Invoke(); };
            tween.SetUpdate(true);
            return tween;
        }

        public static Tweener WindowMove(Transform moveAbleDialogue, UIAnimationData data, UnityAction complete = null)
        {
            if (data.timer==0)
            {
                complete?.Invoke();
                return null;
            }
            //位置下移
            RectTransform moveRect = moveAbleDialogue.GetComponent<RectTransform>();
            moveRect.DOKill();
            moveRect.anchoredPosition = data.start;
            var tweener = moveRect.DOAnchorPos(data.end, data.timer);
            //星星结算界面的初始位置
            tweener.SetEase(data.easeType).SetDelay(data.delay).onComplete = () => { complete?.Invoke(); };
            tweener.SetUpdate(true);
            return tweener;
        }

        public static Tweener Fade(CanvasGroup canvas, UIAnimationData data, UnityAction complete = null)
        {
            canvas.DOKill();
            if (data.timer > 0f)
            {
                canvas.alpha = data.startAlpha;
                return canvas.DOFade(data.endAlpha, data.timer).SetEase(data.easeType).SetUpdate(true);
            }

            return null;
        }
    }

    public enum UIAnimationType
    {
        Scale = 1,
        Move = 2,
    }
}